﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.DataBase.FrameWork.Plugin;

namespace olierFrm.DataBase.FrameWork
{
    public class DefinedSQL : IDisposable
    {
        public DefinedSQL() { }

        private SqlHelper _SqlHelper;
        private List<ObjectSQLParam> _ObjectSQLParam;
        private List<ObjectSQL> _ObjectSQL;
        private string _SqlID;
        private FrameWorkEntity _DBEntity;
        internal SqlHelper SqlHelper
        {
            get
            {
                if (this._SqlHelper == null)
                {

                    this._SqlHelper = new SqlHelper();
                }
                return this._SqlHelper;
            }
        }
        public List<ObjectSQLParam> ObjectSQLParam
        {
            get
            {
                if (this._ObjectSQLParam == null)
                {
                    this._ObjectSQLParam = new List<ObjectSQLParam>();
                }
                return this._ObjectSQLParam;
            }
            set
            {
                this._ObjectSQLParam = value;
            }
        }
        protected List<ObjectSQL> ObjectSQL
        {
            get
            {
                if (this._ObjectSQL == null)
                {
                    this._ObjectSQL = new List<ObjectSQL>();
                }
                return this._ObjectSQL;
            }
            set
            {
                this._ObjectSQL = value;
            }
        }
        protected string SqlID
        {
            get
            {
                return this._SqlID;
            }
            set
            {
                this._SqlID = value;
            }
        }
        internal FrameWorkEntity DBEntity
        {
            get
            {
                if (this._DBEntity == null)
                {
                    this._DBEntity = new FrameWorkEntity(ConnectionConfig.FrmEntityConnectionStr);
                }
                return this._DBEntity;
            }
        }
        public ObjectSQLParam FindParamByName(string ParamName)
        {
            var p =
                from pp in this.ObjectSQLParam
                where pp.ParamName == ParamName
                select pp;
            if (p.Count<ObjectSQLParam>() != 0)
            {
                return p.First<ObjectSQLParam>();
            }
            return null;
        }
        public void SetParam(string ParamName, object ParamValue)
        {
            List<ObjectSQLParam> op = (
                from o in this.ObjectSQLParam
                where o.ParamName == ParamName
                select o).ToList<ObjectSQLParam>();
            if (op.Count<ObjectSQLParam>() == 0)
            {
                throw new FrameWorkError
                {
                    SqlID = this.SqlID,
                    ErrorType = "重复参数赋值时",

                    Summary = ParamName + "参数没有定义"
                };
            }
            op.First<ObjectSQLParam>().ParamValue = ParamValue;
        }



        public void Dispose()
        {
            SqlHelper.CloseConnecton();
        }
    }
}
